package eu.boss.activities;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;

import Util.FinishListener;
import Util.Util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import eu.boss.R;
import eu.boss.controller.ShadowStatsController;
import eu.boss.http.Http;
import eu.boss.model.PlayersList;

public class SplashScreenActivity extends Activity {


	private ShadowStatsController mController;
	
	private ServiceConnection mControllerConnection = new ServiceConnection() {
		// Called when the connection with the service is established
		public void onServiceConnected(ComponentName className, IBinder service) {
			mController = ((ShadowStatsController.LocalBinder) service)
					.getService();
			if (Util.isOnline(SplashScreenActivity.this))
			{
				PlayersGetTask pt = new PlayersGetTask();
				pt.execute();
			}
			else
				displayErrorMessageAndFinish(getString(R.string.connection_error));
		}

		// Called when the connection with the service disconnects unexpectedly
		public void onServiceDisconnected(ComponentName className) {
			mController = null;
		}
	};
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splashscreen);
	}
	
	
	@Override
	public void onStart() {
		super.onStart();
		// new intent to bind service
		Intent intent = new Intent(this, ShadowStatsController.class);
		this.bindService(intent, mControllerConnection, Context.BIND_AUTO_CREATE);
	}
	
	
	/**
	 * AsyncTask making HTTP Get request (second thread)
	 * 
	 * @param dialog
	 *            Dialog displayed during the request
	 * @author Arnaud
	 * 
	 */
	private class PlayersGetTask extends AsyncTask<Void, Void, PlayersList> {

		@Override
		protected PlayersList doInBackground(Void... params) {

			PlayersList list = null;
			/** PlayersListRequest */
			Http getRequest = new Http();
			String url = getString(R.string.web_app_url);
			try {
				list = getRequest.executeGetRequest(url);

			} catch (JsonParseException e) {
				displayErrorMessage(e.toString());
			} catch (JsonMappingException e) {
				displayErrorMessage(e.toString());
			} catch (IllegalStateException e) {
				displayErrorMessage(e.toString());
			} catch (IOException e) {
				displayErrorMessage(e.toString());
			}
			return list;
		}

		@Override
		protected void onPostExecute(PlayersList p) {
			mController.setPlayersList(p);
			finish();
			startActivity(new Intent (SplashScreenActivity.this, TabHostActivity.class));			
		}
	};

	/**
	 * Displaying error message in an AlertDialog
	 * 
	 * @param msg
	 *            message to display
	 */
	public void displayErrorMessage(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.error));
		builder.setMessage(msg);
		builder.setPositiveButton(R.string.ok, null);
		builder.show();
	}

	/**
	 * Displaying error message in an AlertDialog and exit activity
	 * 
	 * @param msg
	 *            message to display
	 */
	public void displayErrorMessageAndFinish(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.error));
		builder.setMessage(msg);
		builder.setPositiveButton(R.string.ok, new FinishListener(this));
		builder.show();
	}
}
